<?php
namespace Core\Libs\Url;

class Url {

	final public static function &_urlToVars($url, $tipo = 'post') {

		if(!is_array(&$url)) {

			$url = array();
		}

		foreach($url as $i => $valor) {

			if($tipo === 'post') {

				$_POST[urldecode(strip_tags($i))] =& urldecode(strip_tags($valor));
			}

			if($tipo === 'get') {

				$_GET[urldecode(strip_tags($i))] =& urldecode(strip_tags($valor));
			}

			else {

				$_REQUEST[urldecode(strip_tags($i))] =& urldecode(strip_tags($valor));
			}
		}
	}

	final public static function getUrlBase() {

		return $_GET['url'];
	}

	final public static function convertUrlSerialToArray($url) {

		$url =& explode('&', &$url);

		foreach($url as $i) {

			$arrayIdxVal =& explode('=', $i);

			$array[$arrayIdxVal[0]] =& $arrayIdxVal[1];
		}

		return $array;
	}

	final public static function getUrl($url = NULL, $separador = '-', $separadorParametros = ':') {

		if($url === NULL) {

			$url = self::getUrlBase();
		}

		$url =& urldecode(&$url);
		$urlBase = $url;
		$url =& preg_replace('/^(\/'.DIR_BASE_NAME.'\/?|\/)|\/$/i', '', &$url);
		$urlAssoc =& preg_match('/'.$separador.'/', &$url, $match[0]);
		$urlParams =& preg_match('/'.$separadorParametros.'/', &$url, $match[1]);
		$arrayUrlAssoc = array();
		$url = explode('/', &$url);
		$auxUrl = $url;
		unset($auxUrl[0], $auxUrl[1]);
		$auxUrl =& array_values(&$auxUrl);
		$arrayParamsSubstituidos = array();

		$countUrl =& count(&$url);
		$i = 0;
		$j = 1;
		$k = 0;
		$l = 1;

		for(;$i < $countUrl; $i+=2, $j+=2) {

			$arrayUrlParalelo[$url[$i]] = $url[$j];

			if($k < $countUrl) {

				$arrayUrlIndexadoParalelo[$url[$k]] = $url[$l];

				$k += 1;
				$l += 1;
			}
		}

		$i = 0;
		$j = 1;

		for(;$i < $countUrl; $i++) {

			if($urlParams && $separador !== $separadorParametros) {

				if(preg_match('/'.$separadorParametros.'/', &$url[$i])) {

					$auxArrayParams =& explode($match[1][0], $url[$i], 2);
					$stringParam = $auxArrayParams[0].$separadorParametros.$auxArrayParams[1];
					$flagParam =& array_search($stringParam, &$auxUrl);

					if($flagParam) {

						$arrayParamsSubstituidos[] =& $auxUrl[$flagParam];
						$arrayParams[$auxArrayParams[0]] = $auxArrayParams[1];
						unset($auxUrl[$flagParam]);
					}

					elseif(!empty($auxArrayParams[0])) {

						$arrayParams[$auxArrayParams[0]] = $auxArrayParams[1];
					}
				}
			}

			else {

				$arrayParams = $arrayUrlAssoc;
			}

			$j = $url[$i];	
			$arrayUrlIndexado[$j] = $j;
		}

		return array('parallel' => &$arrayUrlParalelo, 'indexed' => &$arrayUrlIndexado, 'indexedInParallel' => &$arrayUrlIndexadoParalelo, 'partitioned' => &$url, 'base' => &$urlBase, 'params' => array('assoc' => $arrayParams, 'numeric' => &$auxUrl));
	}
}
?>